// pages/share/detail/detail.js
const API = require('../../../utils/api')
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: 2,
        detail: {},
        content:  ``, 
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        if( options && options.id ){
            console.log( 'options', options)
            this.setData({
                type: options.type,
            })
            this.get_artcle_detail( options.id )
            if( options.userid ){ //分享打开赋值绑定
                app.globalData.inviterUserId = options.userid
                app.globalData.qrQuery = options
            }
        }else if( app.globalData.qrQuery && app.globalData.qrQuery.id ){
            // console.log( 'globalData', app.globalData.qrQuery)
            this.get_artcle_detail( app.globalData.qrQuery.id )
        }

        //分享时 未登录先跳转登陆
        if( !app.globalData.hasUserInfo ){
            wx.showToast({
                title: '您还没有登陆哦',
                icon:"none"
            })
              
            setTimeout( () => {
                wx.reLaunch({
                    url: '/pages/login/login?back=/pages/share/detail/detail',
                })
            },3000)
            return
        }

    },

    /**
     * 接口 - 获取文章详情
     */
    get_artcle_detail:function( id ){
        API.get_artcle_detail({
            id:id
        }).then( res => {
            if( res.code != 200 ){
                return
            }
            let detail = res.data
            detail.content = detail.content.replace(/\<img/gi, '<img style="max-width:100%;height:auto"')
            // console.log(detail.content)
            this.setData({
                detail: detail
            })
        })
    },



    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        let that = this
        let userid = app.globalData.userInfo.id
        console.log(userid)
        return{
            title: that.data.detail.title,
            path: 'pages/share/detail/detail?userid=' + userid + '&type=2&id=' + that.data.detail.id,
            imageUrl: that.data.detail.logoImg,
        }
    },
    onShareTimeline: function(){
        let that = this
        let userid = app.globalData.userInfo.id
        console.log(userid)
        return{
            title: that.data.detail.title,
            query: 'userid=' + userid + '&type=2&id=' + that.data.detail.id,
            imageUrl: that.data.detail.logoImg,
        }
    }
})